package com.gxa.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.gxa.common.JsonResult;
import com.gxa.pojo.Customer;
import com.gxa.service.CustomerService;
import com.gxa.service.impl.CustomerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/customer/update")
public class CustomerUpdateServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取表单数据
        Integer id = Integer.parseInt(request.getParameter("id"));
        Long customer_code = Long.parseLong(request.getParameter("customer_code"));
        //        System.out.println(customer_code+"...");
        String customer_name = request.getParameter("customer_name");
        Integer sex = Integer.parseInt(request.getParameter("sex"));
        String birth = request.getParameter("birth");
        String type = request.getParameter("type");
        Integer status = Integer.parseInt(request.getParameter("status"));
        Long mobile_phone = Long.parseLong(request.getParameter("mobile_phone"));
        Long office_phone = Long.parseLong(request.getParameter("office_phone"));
        String email = request.getParameter("email");
        Long home_phone = Long.parseLong(request.getParameter("home_phone"));
        String address = request.getParameter("address");
        String remark = request.getParameter("remark");
        String entry_time = request.getParameter("entry_time");
        String sale_admin = request.getParameter("sale_admin");
        //创建Customer对象
        Customer customer = new Customer();
        customer.setId(id);
        customer.setCustomer_code(customer_code);
        customer.setCustomer_name(customer_name);
        customer.setSex(sex);
        customer.setBirth(birth);
        customer.setType(type);
        customer.setStatus(status);
        customer.setMobile_phone(mobile_phone);
        customer.setOffice_phone(office_phone);
        customer.setEmail(email);
        customer.setHome_phone(home_phone);
        customer.setAddress(address);
        customer.setRemark(remark);
        customer.setEntry_time(entry_time);
        customer.setSale_admin(sale_admin);
        //调用service
        CustomerService customerService = new CustomerServiceImpl();
        JsonResult jsonResult = customerService.updateCustomer(customer);
        //响应jsonResult
        response.setContentType("application/json; charset=UTF-8");
        response.getWriter().write(JSONObject.toJSONString(jsonResult));
    }
}
